<!DOCTYPE HTML>
<html><head>
<title>Date Series Inference Test</title>
<script src="date-util.js"></script>
<script src="date-series.js"></script>
<script src="date-series-inference.js"></script>
<style>
.pass { background: #efe }
.fail { background: #fee }
td { white-space: pre-wrap }
</style>
</head>

<body>
<h1>Date Series Inference Test</h1>

<table id=tests cellpadding=4 cellspacing=0 border=1>
  <tr><th>Dates</th><th>Rule</th></tr>
  <tr>
    <td>2012-7-3, 2012-7-5, 2012-7-10, 2012-7-12</td>
    <td>Every week on Tuesdays, and Thursdays</td>
  </tr>
  <tr>
    <td>2012-1-15, 2012-4-15, 2012-7-15</td>
    <td>Every 3 months on the 15th</td>
  </tr>
  <tr>
    <td>2012-1-15, 2012-4-15, 2012-7-15</td>
    <td>{"freq":2,"interval":3,"wkst":0}</td>
  </tr>
  <tr>
    <td>2012-1-31, 2012-4-30, 2012-7-31</td>
    <td>Every 3 months on the last day</td>
  </tr>
  <tr>
    <td>2012-1-27, 2012-2-23, 2012-3-21, 2012-4-17, 2012-5-14</td>
    <td>Every 27 days</td>
  </tr>
  <tr>
    <td>2012-2-10, 2012-2-24, 2012-3-9, 2012-3-23</td>
    <td>Every 2 weeks on Fridays</td>
  </tr>
</table>

<script>(function () {
  function $(id) { return document.getElementById(id); }
  function parseDate(dateStr) {
    var parts = dateStr.split(/-/g);
    return new Date(Date.UTC(+parts[0], parts[1] - 1, +parts[2]));
  }

  function checkResult(cell, golden, actual) {
    cell.className = (golden === actual) ? 'pass' : 'fail';
    if (golden !== actual) {
      cell.appendChild(cell.ownerDocument.createElement("hr"));
      cell.appendChild(cell.ownerDocument.createTextNode(actual));
    }
  }

  var table = $('tests');
  var rows = table.getElementsByTagName("tr");
  for (var i = 0, n = rows.length; i < n; ++i) {
    var cells = rows[i].getElementsByTagName("td");
    if (cells.length === 0) { continue; }
    var datesStr = cells[0].textContent;
    var golden = cells[1].textContent;

    var dates = datesStr.split(/,\s*/g);
    (function () {
      for (var i = dates.length; --i >= 0;) {
        dates[i] = parseDate(dates[i]);
      }
    })();

    var result = inferDateSeries(dates);
    var actual;
    if (/^\{/.test(golden)) {
      actual = JSON.stringify(result.rule);
      golden = JSON.stringify(JSON.parse(golden));
    } else if (result.confidence >= .75) {
      actual = dateSeries(result.dates[0], result.rule).toString();
    } else {
      var nResultDates = result.dates.length;
      actual = nResultDates + " date" + (nResultDates === 1 ? "" : "s");
    }

    checkResult(cells[1], golden, actual);
  }
})();</script>

</body></html>
